//
// $.ui.dialog.maxZ - максимальный z-index
//
function app_class() {
    this.paginator = { current:0, total:0 };
	this.customer = { uname:'', cust_id:0 };
};

app_class.prototype.alert = function (dlgID, title, msg, icon, doing, param) {
    var aldlg = $('#' + dlgID);
    var newdlg = $('#' + dlgID + '_mess').length ? false : true;

    if (newdlg)
        aldlg.html(
'<table id="' + dlgID + '_mess" style="display:none; border:thin solid transparent; height:100%; width:100%"><tr>' +
'    <td style="padding-left:64px; padding-right:10px; vertical-align:middle;"></td>' +
'</tr></table>'
);

    aldlg.dialog(
        {
            modal: true, title: title,
            zIndex: app.alert_min_z_index,
            height: 180,
            width: 360,
            autoOpen: false,
            resizable: false,
            open: function () { $('div[aria-labelledby="ui-dialog-title-' + dlgID + '"]').shadow() },
            close: function () {
                $(this).dialog("destroy");
                $.ui.dialog.maxZ = $.maxZIndex({ group: '.ui-dialog.ui-widget' });
            },
            buttons: { "Закрыть": function () { 
                    $(this).dialog("close");
                    if (doing) doing(param);
                } 
            }
        }
    );
    aldlg.before('<div id="' + dlgID + '_icon" class="' + icon + '" ' +
        'style="display:none;position:absolute;left:16px;top:64px;"></div>');

    $('#' + dlgID + '_mess td').html(msg);
    aldlg.dialog('open');
    $('#' + dlgID + '_icon').show();
    $('#' + dlgID + '_mess').show();
}

app_class.prototype.confirm = function( dlgID, title, msg, icon, doing, param ) {
var aldlg = $('#'+dlgID);
var newdlg = $('#'+dlgID+'_mess').length ? false : true;

if( newdlg )
    aldlg.html(
'<table id="'+dlgID+'_mess" style="display:none; border:thin solid transparent; height:100%; width:100%"><tr>'+
'    <td style="padding-left:64px; padding-right:10px; vertical-align:middle;"></td>'+
'</tr></table>'
);

    aldlg.dialog( 
        {
            modal: true, title: title,
            zIndex: app.alert_min_z_index,
            height: 180, width: 360,
            autoOpen: false, resizable: false,
            open: function () { $('div[aria-labelledby="ui-dialog-title-' + dlgID + '"]').shadow() },
            close: function () {
                $(this).dialog("destroy");
                $.ui.dialog.maxZ = $.maxZIndex({ group: '.ui-dialog.ui-widget' });
            },
            buttons: {
                "Да": function () {
                        $(this).dialog("close");
                        doing(param); 
                    },
                "Нет": function () {
                        $(this).dialog("close");
                    }
            }
        }
    );
    aldlg.before( '<div id="'+dlgID+'_icon" class="'+icon+'" '+
        'style="display:none;position:absolute;left:16px;top:64px;"></div>' );    

    $('#'+dlgID+'_mess td').html(msg);
    aldlg.dialog('open');
    $('#'+dlgID+'_icon').show();
    $('#'+dlgID+'_mess').show();
}

app_class.prototype.help = function( dlgID, title, app ) {
var aldlg = $('#'+dlgID);
var newdlg = $('#'+dlgID+'_HelpContent').length ? false : true;

    if( newdlg ) {
        aldlg.css({'overflow':'hidden', 'padding':'0px', 'padding-bottom':'14px' });
        aldlg.html( '<iframe id="'+dlgID+'_HelpContent" src=""'+
                    ' style="width:100%; height:100%; border: 1px solid #aaaaaa"></iframe>' );
        aldlg.dialog( {
            modal: false, title: title,
            zIndex: app.ui_dialog_min_z_index,
            height: 500, width: 680,
            position: ['right','bottom'],
            autoOpen: false, resizable: true,
            open: function () { $('div[aria-labelledby="ui-dialog-title-' + dlgID + '"]').shadow() },
            close: function () {
                $(this).dialog("destroy");
                $.ui.dialog.maxZ = $.maxZIndex({ group: '.ui-dialog.ui-widget' });
            }
        } );
    }

    aldlg.dialog('open');
    
    ts = app.apptasks;
    HelpUrl = app.defhelpurl;
    for(i=0; i<ts.length; i++ ) if( ts[i].active && ts[i].helpurl ) { HelpUrl = ts[i].helpurl; break; }
    
    $('#'+dlgID+'_HelpContent').attr( 'src', HelpUrl );
}

app_class.prototype.ShowImage = function( img_id, photo_src, title) {
    if( photo_src == '' || img_id == '' ) return; 
    
    if($('#'+img_id).html()) {
        $('#'+img_id).dialog('moveToTop');
        return;
    }
    
    $('body').append('<div id="'+img_id+'" style="display:none"><img src="'+photo_src+'"></div>');

    var im = $('#'+img_id+' img');
    im.bind( 'load', function() {
        $('#'+img_id).dialog({
            title: title,
            modal: false,
            resizable: false,
            height: 'auto',
            width: 'auto',
//			show: { effect:'slide', speed:5000},
            stack: true,
			open: function () { $('div[aria-labelledby="ui-dialog-title-' + img_id + '"]').shadow() },
            close: function() {
                    $('#'+img_id).dialog("destroy");
                    $('#'+img_id).remove();
                 }
        });
        
        $('#'+img_id).parent().resizable( {'aspectRatio':true, 'alsoResize':im } );
    });
}                    

app = new app_class();

app.ui_dialog_min_z_index = 100;
app.jqgrid_form_min_z_index = 8000;
app.alert_min_z_index = 9000;
